<?php

// 限时抢购

namespace app\shop\controller;

use think\Request;
use think\Controller;
use app\shop\model\Ad as AdModel;
use app\shop\model\Limited as LimitedModel;
use app\shop\model\GoodsType as GoodsTypeModel;
use app\shop\model\ LimitedType as LimitedTypeModel;

class Limited extends Controller
{
    /**
     * 限时抢购列表
     * @param Request $request
     * @param GoodsTypeModel $goodsType
     * @param LimitedModel $limits
     * @param AdModel $ad
     * @return mixed
     */
    public function index(Request $request, GoodsTypeModel $goodsType, LimitedModel $limits, AdModel $ad)
    {
        // 获取分类参数
        $inquire = $request->param();

        // 限时抢购
        $limited = $limits->with('ShopGoods')->order(['create_time' => 'desc'])->paginate(12, false, ['query' => $inquire]);

        foreach ($limited as &$value) {
            $num = round($value['purchase_number'] / $value['number'], 2) * 100;
            $value['percentage'] = $num . '%';
        }

        // 限时抢购剩余时间
        $end_time = (new LimitedTypeModel())->order(['end_time' => 'desc'])->value('end_time');


        return $this->fetch('', [
            'mate_head' => '限时抢购',
            'ad'        => $ad->where('parenTid', 5)->find(),
            'end_time'  => $end_time,
            'limits'    => $limited,
        ]);
    }
}